#!/bin/bash

#默认服务器配置项
#    "环境名称 docker环境文件"
CONFIGS=(
    "inscode_nodev20环境 node20 Dockerfile-inscode-nodev20"
    "inscode_nodev18环境 node18 Dockerfile-inscode-nodev18"
    "inscode_nodev16环境 node16 Dockerfile-inscode-nodev16"
    "inscode_nodev14环境 node14 Dockerfile-inscode-nodev14"
    "inscode_pythonv3环境 python3 Dockerfile-inscode-pythonv3"
    "inscode_pythonv310环境 python310 Dockerfile-inscode-pythonv310"
    "inscode_pythonv311环境 python311 Dockerfile-inscode-pythonv311"
    "inscode_pythonv312环境 python312 Dockerfile-inscode-pythonv312"
    "inscode_pythonv313环境 python313 Dockerfile-inscode-pythonv313"
    "inscode_pythonv2环境 python2 Dockerfile-inscode-pythonv2"
    "inscode_golangv1.19环境 golang19 Dockerfile-inscode-golangv1.19"
    "inscode_golangv1.17环境 golang17 Dockerfile-inscode-golangv1.17"
    "inscode_golangv1.15环境 golang15 Dockerfile-inscode-golangv1.15"
    "inscode_phpv7环境 php7 Dockerfile-inscode-phpv7"
    "inscode_phpv5环境 php5 Dockerfile-inscode-phpv5"
    "inscode_javav1.8环境 java1.8 Dockerfile-inscode-javav1.8"
    "inscode_rust环境 rust Dockerfile-inscode-rust"
    "all环境 all all"
)

docker_env=""
docker_version=""

#服务器配置数
CONFIG_LENGTH=${#CONFIGS[*]}  #配置站点个数

if [[ $CONFIG_LENGTH -le 0 ]] ;
then
    echo "未检测到服务器配置项!"
    echo "请在脚本CONFIGS变量中配置或单独创建一个server_config文件并配置"
    exit ;
fi

echo "此为InsCode镜像 BUILD 脚本，请根据实际情况，选择BUILD镜像版本和环境"
echo "若要push已经打好的镜像，请确认已经登录. "

function ConfigList(){
    for ((i=0;i<${CONFIG_LENGTH};i++));
    do
        CONFIG=(${CONFIGS[$i]}) #将一维sites字符串赋值到数组
        serverNum=$(($i+1))
        echo "(${serverNum}) ${CONFIG[0]}(${CONFIG[1]})"
    done
}

#build 镜像菜单
function BuildMenu(){
    if [  ! -z $docker_env ] && [ ! -z $docker_version ]; then
        AutoBuild $docker_env $docker_version
    else
        echo "*******请输入build镜像的序号&版本******"
        ConfigList
        echo "请输入您选择的build镜像的序号: "
        read docker_env
        echo "请输入您选择的build镜像的版本: "
        read docker_version
    fi
}

#自动BUILD
function AutoBuild(){
    if [ $CONFIG_LENGTH == $1 ]; then
        echo "开始编辑所有环境容器镜像"
        for i in `seq 1 $CONFIG_LENGTH`;do
            num=$(($i-1))
            CONFIG=(${CONFIGS[$num]})
            docker_file=${CONFIG[2]}
            docker_name=${CONFIG[1]}
            if [ "all" != "$docker_name" ]; then
                echo ${CONFIG[1]}
                echo "正在build【${CONFIG[0]}】 swr.cn-north-4.myhuaweicloud.com/gitcode-ai/${docker_name}:v$2"
                docker build -t swr.cn-north-4.myhuaweicloud.com/gitcode-ai/${docker_name}:v$2 -f $docker_file .
            fi
        done
	echo "push镜像命令如下: "
        for i in `seq 1 $CONFIG_LENGTH`;do
            num=$(($i-1))
            CONFIG=(${CONFIGS[$num]})
            docker_file=${CONFIG[2]}
            docker_name=${CONFIG[1]}
            if [ "all" != "$docker_name" ]; then
                echo "docker push swr.cn-north-4.myhuaweicloud.com/gitcode-ai/${docker_name}:v$2"
            fi
        done
        echo "pull镜像命令如下: "
        for i in `seq 1 $CONFIG_LENGTH`;do
            num=$(($i-1))
            CONFIG=(${CONFIGS[$num]})
            docker_file=${CONFIG[2]}
            docker_name=${CONFIG[1]}
            if [ "all" != "$docker_name" ]; then
                echo "docker pull swr.cn-north-4.myhuaweicloud.com/gitcode-ai/${docker_name}:v$2"
            fi
        done
    else
        num=$(($1-1))
        CONFIG=(${CONFIGS[$num]})
        echo ${CONFIG[1]}
        echo "正在build【${CONFIG[0]}】"
        docker_file=${CONFIG[2]}
        docker_name=${CONFIG[1]}
        docker build -t swr.cn-north-4.myhuaweicloud.com/gitcode-ai/${docker_name}:v$2 -f $docker_file .
        echo "push镜像命令如下: "
        echo "docker push swr.cn-north-4.myhuaweicloud.com/gitcode-ai/${docker_name}:v$2"
        echo "pull镜像命令如下: "
        echo "docker pull swr.cn-north-4.myhuaweicloud.com/gitcode-ai/${docker_name}:v$2"
    fi
}

#选择BUILD镜像
function ChooseServer(){

    if [[ $docker_env -gt $CONFIG_LENGTH ]] ;
    then
        echo "输入的序号不正确，请重新输入:"
        ChooseServer ;
        return ;
    fi
    if [[ $docker_env -lt 1 ]] ;
    then
        echo "输入的序号不正确，请重新输入:"
        ChooseServer ;
        return ;
    fi

    if [[ ! -z $docker_version ]]; then
        AutoBuild $docker_env $docker_version;
    else
        echo "输入的序号或版本不正确，请重新输入:"
        echo  $docker_env $docker_version
        ChooseServer ;
        return ;
    fi
}

#读取自定义服务器配置文件（server_config）列表，合并服务器配置列表
if [ -f server_config ]; then
	while read line
	do
		CONFIGS+=("$line")
	done < server_config
fi

# 程序入口
if [ 2 == $# ]; then
    ConfigList
    docker_env=$1
    docker_version=$2
    if [ ! -z  $docker_env ] && [ ! -z $docker_version ]; then
        AutoBuild $docker_env $docker_version
    else
        echo "请输入镜像序号："
        read docker_env

        echo "请输入镜像版本："
        read docker_version
        AutoBuild $docker_env $docker_version
    fi
else
    BuildMenu
    ChooseServer
fi